package com.neutech.maplestory.entity;

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.ImageUtil;

import java.awt.*;
import java.util.Random;

import static com.neutech.maplestory.entity.Action.STAND;
import static com.neutech.maplestory.entity.Action.WALK;
import static com.neutech.maplestory.entity.Direction.LEFT;
import static com.neutech.maplestory.entity.Direction.RIGHT;

/**
 * @author ZZQ
 * @PackageName: com.neutech.maplestory.entity
 * @ClassName: Mob
 * @Description:
 * @date 2021/1/28 19:58
 */
public class Mob extends AbstractMapleStoryObject{
    public static Image[]imgs = new Image[100];
    static{
        for (int i = 0; i < 6; i++) {
            imgs[i] = ImageUtil.getImage("mob01_left_stand" + i);
        }
        for (int i = 6; i < 12; i++) {
            imgs[i] = ImageUtil.getImage("mob01_left_walk" + (i - 6));
        }
        for (int i = 12; i < 19; i++) {
            imgs[i] = ImageUtil.getImage("mob01_left_die" + (i - 12));
        }
        imgs[19] = ImageUtil.getImage("blood");
        for(int i = 20;i < 26;i ++){
            imgs[i] = ImageUtil.getImage("mob01_right_stand" + (i - 20));
        }
        for(int i = 26;i < 32;i ++){
            imgs[i] = ImageUtil.getImage("mob01_right_walk" + (i - 26));
        }
        for(int i =32;i < 38;i ++){
            imgs[i] = ImageUtil.getImage("mob01_right_die" + (i - 32));
        }

    }
    /**
     * 最大血量
     */
    public int MAX_HP;
    /**
     * 基础攻击力
     */
    public int ATT;
    /**
     * 伤害值
     */
    public int hitValue;

//    public Random r = new Random();
//    int rd = r.nextInt(1);
    public Mob(){
        this.width = imgs[0].getWidth(null);
        this.height = imgs[0].getHeight(null);
        this.dir = LEFT;
        this.action = STAND;
        this.live = true;
        this.MAX_HP = 10000;
        this.HP = MAX_HP;
        this.ATT = 200;
//        this.speed = 5;
    }
    public Mob(MapleStoryClient msc,int x,int y,int speed){
        this();
        this.msc = msc;
        this.x = x;
        this.y =y;
        this.speed = speed;
    }
    private int count;
    private int step;
    private int diestep;
    @Override
    public void draw(Graphics g) {
        if(diestep >= 6){
            this.live = false;
            diestep = 0;
        }
        if(!live){
            msc.mobs.remove(this);
            return;
        }

//        super.draw(g);

//        if(count ++ % 3 == 0 ){
//            g.drawImage(imgs[step ++ % 3 ],x,y,null);
//        }else {
//            g.drawImage(imgs[step % 3 ],x,y,null);
//        }
        switch (dir){
            case LEFT:
                switch (action){
                    case STAND:
                        if(count ++ % 6 == 0 ){
                            g.drawImage(imgs[step ++ % 6 ],x,y,null);
                        }else {
                            g.drawImage(imgs[step % 6 ],x,y,null);
                        }
                        break;
                    case WALK:
                        if(count ++ % 6 == 0 ){
                            g.drawImage(imgs[(step ++ % 6) + 6 ],x,y,null);
                        }else {
                            g.drawImage(imgs[step % 6 + 6 ],x,y,null);
                        }
                        break;
                    case DIE:
                        if(count ++ % 6 == 0 ){
                            g.drawImage(imgs[(diestep ++ % 7) + 12 ],x,y,null);
                        }else {
                            g.drawImage(imgs[diestep % 7 + 12 ],x,y,null);
                        }
                        break;
                    default:
                        break;
                }
                break;
            case RIGHT:
                switch (action){
                    case STAND:
                        if(count ++ % 6 == 0){
                            g.drawImage(imgs[(step ++ % 6) + 20],x,y,null);
                    }else{
                            g.drawImage(imgs[step % 6 + 20],x,y,null);
                        }
                        break;
                    case WALK:
                        if(count ++ % 6 == 0){
                            g.drawImage(imgs[(step ++ % 6) + 26],x,y,null);
                        }else{
                            g.drawImage(imgs[step % 6 + 26],x,y,null);
                        }
                        break;
                    case DIE:
                        if (count ++ % 6 == 0){
                            g.drawImage(imgs[(diestep ++ % 6) + 32],x,y,null);
                        }else{
                            g.drawImage(imgs[diestep % 6 + 32],x,y,null);
                        }
                        break;
                }
                break;
            default:
                break;
        }if(HP < MAX_HP){
            bloodBar.draw(g);
        }
//        bloodBar.draw(g);
        move();
        change();
        outOfBound();
    }

    private void outOfBound(){
        if(this.x >= Constant.GAME_WIDTH - this.width && this.y >= 480){
            this.x = Constant.GAME_WIDTH - this.width;
            this.dir = Direction.LEFT;
        }
        if(this.x <= 0 && this.y >= 480){
            this.x = 0;
            this.dir = Direction.RIGHT;
        }
        if( this.x <= 180 && this.y <= 100 ){
            this.x = 180;
            this.dir = Direction.RIGHT;
        }
        if(this.x >= 1105 - this.width && this.y <= 100){
            this.x = 1105 - this.width;
            this.dir = Direction.LEFT;
        }
        if( this.x <= 480 && this.y >= 100 && this.y <= 480){
            this.x = 480;
            this.dir = Direction.RIGHT;
        }
        if(this.x >= 1105 - this.width && this.y >= 100 && this.y <= 480){
            this.x = 1105 - this.width;
            this.dir = Direction.LEFT;
        }
    }
    Random r = new Random();
    public void change() {
        if (count % 50 == 0 && this.action != action.DIE) {
            int rd = r.nextInt(4);
            switch (rd) {
                case 0:
                    this.dir = LEFT;
                    this.action = Action.STAND;
                    break;
                case 1:
                    this.dir = LEFT;
                    this.action = Action.WALK;
                    break;
                case 2:
                    this.dir = RIGHT;
                    this.action = Action.STAND;
                    break;
                case 3:
                    this.dir = RIGHT;
                    this.action = Action.WALK;
                    break;
                default:
                    break;

            }
        }
    }

    @Override
    public void move() {
        switch (dir){
            case LEFT:
                switch (action){
                    case WALK:
                        this.x -= speed;
                       break;
                    default:
                        break;
                }
                break;
            case RIGHT:
                switch (action) {
                    case WALK:
                        this.x += speed;
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
    }
    public BloodBar bloodBar =new BloodBar();
    class BloodBar extends AbstractMapleStoryObject{
//        public BloodBar(){}
        private Image img;
        public BloodBar(){
            this.img = ImageUtil.getImage("blood");
            this.width = img.getWidth(null);
//            this.width = Mob.this.width;
//            this.height = 10;
        }
        @Override
        public void draw(Graphics g) {
//            //框
//            //内部类访问外部类相同属性名时，使用外部类.this.属性名
//            Color c = g.getColor();
//
//            g.drawRect(Mob.this.x,Mob.this.y - 15,Mob.this.width,10);
//            //填充
//            g.setColor(Color.RED);
//            g.fillRect(Mob.this.x + 1,Mob.this.y - 15 + 1,(int)(Mob.this.width * Mob.this.HP / Mob.this.MAX_HP),10 - 1);
//            g.setColor(c);
            int n = Mob.this.width / this.width * Mob.this.HP / Mob.this.MAX_HP;
            for(int i= 0;i < n;i ++) {
                g.drawImage(Mob.this.imgs[19], Mob.this.x + (i * this.width), Mob.this.y - 15, null);
            }
//            Font f = g.getFont();
//            g.setFont(new Font("微软雅黑",Font.BOLD,60));
//            g.drawString(Mob.this.msc.hero.hitValue + "",Mob.this.x,Mob.this.y);
//            g.setFont(f);
        }
    }
}


